package exttable.formgenerator;

import utils.Localisation;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 03.11.2010
 * Time: 16:49:04
 * To change this template use File | Settings | File Templates.
 */
public class MessageFactory {

    public static boolean isToSave() {
        int YES = 0;
        Object[] options = {Localisation.getMessage("form.yes"), Localisation.getMessage("form.no")};

        int choice = JOptionPane.showOptionDialog(null
                , Localisation.getMessage("warring.datawillbeadded")
                , Localisation.getMessage("warring.waring")
                , JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
        return choice == YES;
    }

    public static void errorToSave() {
        JOptionPane.showMessageDialog(null, Localisation.getMessage("error.datawrite"));
        JOptionPane.showMessageDialog(null, Localisation.getMessage("help.closeExcelFile"));
    }

    public static void emptyFields() {
        JOptionPane.showMessageDialog(null, Localisation.getMessage("warring.emptyfields"),
                Localisation.getMessage("warring.waring"),
                JOptionPane.WARNING_MESSAGE);
    }

    public static boolean done(boolean b) {
        if (b) {
            JOptionPane.showMessageDialog(null, Localisation.getMessage("info.dataadded"), "",
                    JOptionPane.INFORMATION_MESSAGE);
            return true;
        } else {
            JOptionPane.showMessageDialog(null, Localisation.getMessage("errorDone"));
        }
        return false;
    }

    public static boolean isEmptyField(Component[] componentList) {
        for (Component component : componentList) {
            if (component instanceof JTextComponent) {
                if (((JTextComponent) component).getText().length() < 1) {
                    MessageFactory.emptyFields();
                    component.requestFocus();
                    return true;
                }
            }
            else if(component.getClass().getSimpleName().equals("JScrollPane")){
                JScrollPane jScrollPane=(JScrollPane)component;
                if(jScrollPane.getViewport().getView() instanceof JList){
                    JList jList=(JList)jScrollPane.getViewport().getView();
                    if (jList.isSelectionEmpty()) {
                        MessageFactory.emptyFields();
                        jList.requestFocus();
                        return true;
                    }
                }

            }
        }
        return false;
    }

    public static void getMessageWarnings(boolean status){
        if(status){
          JOptionPane.showMessageDialog(null,Localisation.getMessage("warningFounds"),
                  Localisation.getMessage("warring.waring"),JOptionPane.WARNING_MESSAGE);
        }else{
          JOptionPane.showMessageDialog(null, Localisation.getMessage("NoWarningFounds"),
                  Localisation.getMessage("info.title"),
                    JOptionPane.INFORMATION_MESSAGE);
        }
    }

    public static boolean areYouSure(String message) {
        int YES = 0;
        Object[] options = {Localisation.getMessage("form.yes"), Localisation.getMessage("form.no")};

        int choice = JOptionPane.showOptionDialog(null
                , message
                , Localisation.getMessage("warring.waring")
                , JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]);
        return choice == YES;
    }

    public static void getMessage(String message) {
        JOptionPane.showMessageDialog(null, message);
    }
}


